package com.aeric.qrpro;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.aeric.CWNetwork.BaseActivity;
import com.aeric.CWUtility.SharedPreferencesUtils;
import com.aeric.CWUtility.ToastUtil;
import com.aeric.domain.UserInfo;



public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener,View.OnClickListener{
    public Button scanBtn;
    private Context mContext;
    private TextView tv_user_name;
    private long firstBackKeyTime = 0;
    private final static int USER_CODE = 1003;
    private boolean isNeedDeleteCache = true;
    private SharedPreferencesUtils sharedPreferencesUtils;
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        sharedPreferencesUtils = new SharedPreferencesUtils(this,"User");
        setupActionBar();
        initView();
    }

    private void initData() {
        UserInfo ui = sharedPreferencesUtils.getObject("UserInfo",UserInfo.class);
        if(ui!=null) {
            tv_user_name.setText(ui.userName);
        }else {
            tv_user_name.setText("欢迎登录");
        }
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(R.string.main);
        }
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_main);
        tv_user_name = headerLayout.findViewById(R.id.tv_user_name);
        scanBtn = findViewById(R.id.scan);
        scanBtn.setOnClickListener(this);
    }

    public void userlogin(View v){
        String actionName = tv_user_name.getText().toString();
        if(actionName.equals("欢迎登录")){
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
        }else{
            UserInfo ui = sharedPreferencesUtils.getObject("UserInfo",UserInfo.class);
            Intent intent = new Intent(mContext, UserCenterActivity.class);
            intent.putExtra("userInfo",ui);
            startActivity(intent);
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
    }

    @Override
    public void onClick(View v) {
        Intent _intent = new Intent();
        switch (v.getId()) {
            case R.id.scan:
                UserInfo ui = sharedPreferencesUtils.getObject("UserInfo",UserInfo.class);
                if(ui!=null){
                    _intent.putExtra("UserInfo",ui);
                    _intent.setClass(MainActivity.this, AmmeterInfoActivity.class);
                    startActivity(_intent);
               }else{
                   ToastUtil.toastShortTip(this,"请先登录获取权限！");
               }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)// 主要是对这个函数的复写
    {
        // TODO Auto-generated method stub
        if ((keyCode == KeyEvent.KEYCODE_BACK)
                && (event.getAction() == KeyEvent.ACTION_DOWN)) {
            if (System.currentTimeMillis() - firstBackKeyTime > 2000) // 2s内再次选择back键有效
            {
                Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                firstBackKeyTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0); // 凡是非零都表示异常退出!0表示正常退出!
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void needDeleteCache(boolean delete) {
        isNeedDeleteCache = delete;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
